package com.cheng.dreams.sample.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.boc.bottombar.MainBottomBarItemView;
import com.cheng.dreams.sample.R;
import com.cheng.dreams.sample.ui.fragment.BlankFragment;
import com.cheng.dreams.sample.ui.fragment.HomeFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.R.attr.id;

public class MainActivity extends BaseActivity  {

    @BindView(R.id.index)
    MainBottomBarItemView index;
    @BindView(R.id.type)
    MainBottomBarItemView type;
    @BindView(R.id.shopcar)
    MainBottomBarItemView shopcar;
    @BindView(R.id.me)
    MainBottomBarItemView me;
    @BindView(R.id.bottom_bar)
    LinearLayout bottomBar;
    @BindView(R.id.content)
    FrameLayout content;
    private final String TAG_FRAGMENT_INDEX = "index";
    private final String TAG_FRAGMENT_TYPE = "type";
    private final String TAG_FRAGMENT_SHOPCAR = "shopcar";
    private final String TAG_FRAGMENT_ME = "me";

    private Fragment lastFragment = null;
    private int lastChecked = R.id.index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        setEnabled(false, true, true, true);
        changeFragment(TAG_FRAGMENT_INDEX);
    }

    private void changeFragment(String tag) {
        Fragment fragment = getSupportFragmentManager()
                .findFragmentByTag(tag);
        if (fragment == null) {
            fragment = getFragmentByTag(tag);
            addFragment(fragment, tag);
        } else {
            showFragment(fragment);
        }

        lastFragment = fragment;
    }

    private Fragment getFragmentByTag(String tag) {
        if (TextUtils.isEmpty(tag)) {
            return HomeFragment.newInstance();
        }
        if (tag.equals(TAG_FRAGMENT_INDEX)) {
            return HomeFragment.newInstance();
        } else if (tag.equals(TAG_FRAGMENT_TYPE)) {
            return BlankFragment.newInstance();
        } else if (tag.equals(TAG_FRAGMENT_SHOPCAR)) {
            return BlankFragment.newInstance();
        } else if (tag.equals(TAG_FRAGMENT_ME)) {
            return BlankFragment.newInstance();
        }
        return BlankFragment.newInstance();
    }


    private void addFragment(Fragment fragment, String tag) {
        FragmentTransaction trans = getSupportFragmentManager()
                .beginTransaction();
        if (lastFragment != null) {
            trans.hide(lastFragment);
        }
        trans.add(R.id.content, fragment, tag);
        trans.commitAllowingStateLoss();
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction trans = getSupportFragmentManager()
                .beginTransaction();
        if (lastFragment != null) {
            trans.hide(lastFragment);
        }
        trans.show(fragment);
        trans.commitAllowingStateLoss();
    }

    private void setEnabled(boolean mindex, boolean mtype, boolean mshopcar, boolean mme) {
        index.setBarEnable(mindex);
        type.setBarEnable(mtype);
        shopcar.setBarEnable(mshopcar);
        me.setBarEnable(mme);
    }

    @OnClick({R.id.index, R.id.type, R.id.shopcar, R.id.me})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.index:
                setEnabled(false, true, true, true);
                if (lastChecked != R.id.index) {
                    changeFragment(TAG_FRAGMENT_INDEX);
                    lastChecked = id;
                }
                break;
            case R.id.type:
                setEnabled(true, false, true, true);
                if (lastChecked != R.id.type) {
                    changeFragment(TAG_FRAGMENT_TYPE);
                    lastChecked = id;
                }
                break;
            case R.id.shopcar:
                setEnabled(true, true, false, true);
                if (lastChecked != R.id.shopcar) {
                    changeFragment(TAG_FRAGMENT_SHOPCAR);
                    lastChecked = id;
                }
                break;
            case R.id.me:
                setEnabled(true, true, true, false);
                if (lastChecked != R.id.me) {
                    changeFragment(TAG_FRAGMENT_ME);
                    lastChecked = id;
                }
                break;
        }
    }


}
